package com.pignest.hotel.ui.member;

import java.util.List;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Card;
import com.pignest.hotel.beans.Member;
import com.pignest.hotel.global.picker.Picker;
import com.pignest.hotel.global.picker.Receiver;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.UIUtils;

public class MemberPicker extends Window implements Picker<Member> {
	private static final long serialVersionUID = 6060043899143411633L;
	
	public static final String MEMBER = "Member";
	
	private Member member;
	
	private Textbox nameTextbox;
	private Textbox cardTextbox;
	private Textbox phoneTextbox;
	
	private Checkbox pickedCheckbox;
	private Listbox memberListbox;
	
	private Receiver<Member> receiver;
	private Textbox receiverTextbox;
	
	public void init() {
		if (nameTextbox == null) {
			nameTextbox = (Textbox)getFellow("nameField");
			cardTextbox = (Textbox)getFellow("cardField");
			phoneTextbox = (Textbox)getFellow("phoneField");
			pickedCheckbox = (Checkbox)getFellow("pickedCheckbox");
			memberListbox = (Listbox)getFellow("memberListbox");
		}
	}
	
	public void onOK() {
		boolean s = pickedCheckbox.isChecked();
		if (s && member == null) {
			return;
		}
		if (receiver == null && receiverTextbox == null)
			return;
		if (receiver != null) {
			receiver.onPicked(s ? member : null);
		} else if (receiverTextbox != null) {
			receiverTextbox.setReadonly(true);
			receiverTextbox.setValue(s ? member.getDisplayName() : null);
			receiverTextbox.setAttribute(MEMBER, s ? member : null);
		}
		detach();
	}
	
	public void onCancel() {
		detach();
	}
	
	public void onQuery() {
		init();
		String name = nameTextbox.getValue();
		String phone = phoneTextbox.getValue();
		String card = cardTextbox.getValue();
		MemberService service = Services.getMemberService();
		
		List<Member> members = service.listMembers(name, card, phone);
		memberListbox.getItems().clear();
		for (int i = 0; i < members.size(); i++) {
			Member member = members.get(i);
			Listitem listitem = new Listitem();
			listitem.setValue(member);
//			listitem.appendChild(new Listcell(Integer.toString(i+1)));
			listitem.appendChild(new Listcell(member.getName()));
			listitem.appendChild(new Listcell(member.getCardNo()));
			listitem.appendChild(new Listcell(member.getMemberLevel().getMemberlevel()));
			listitem.appendChild(new Listcell(member.getMobile()));
			listitem.appendChild(new Listcell(Utils.toPrice(member.getBalance())));
			listitem.setParent(memberListbox);
		}
	}
	
	public void onSelect() {
		init();
		Member m = (Member)memberListbox.getSelectedItem().getValue();
		this.member = m;
		pickedCheckbox.setChecked(true);
		pickedCheckbox.setLabel(m.getDisplayName());
	}
	
	public void onClear() {
		init();
		nameTextbox.setValue(null);
		cardTextbox.setValue(null);
		phoneTextbox.setValue(null);
	}
	
	@Override
	public Member getPicked() {
		return member;
	}

	@Override
	public void setPicked(Member m) {
		this.member = m;
		if (m != null)
			pickedCheckbox.setLabel(m.getDisplayName());
	}
	
	public void setReceiver(Receiver<Member> receiver) {
		this.receiver = receiver;
	}
	
	public void setReceiverTextbox(Textbox receiverTextbox) {
		this.receiverTextbox = receiverTextbox;
	}
	
	public static void pickMember(Textbox receiver) {
		final MemberPicker win = (MemberPicker) Executions.createComponents("/modules/member/memberPicker.zul", null, null);
		win.init();
		win.setReceiverTextbox(receiver);
		win.setPicked((Member)receiver.getAttribute(MEMBER));
		try {
			win.doModal();
		} catch (Exception e) {
		}
	}
	
//	public MemberPicker getInstance(Receiver<Member> receiver) {
//		MemberPicker win = (MemberPicker) Executions.createComponents("/modules/member/memberPicker.zul", null, null);
//		win.init();
//		win.setReceiver(receiver);
//		win.setPicked(receiver.getPicked());
//		return win;
//	}
//	
	public static void pickMember(Receiver<Member> receiver) {
		final MemberPicker win = (MemberPicker) Executions.createComponents("/modules/member/memberPicker.zul", null, null);
		win.init();
		win.setReceiver(receiver);
		win.setPicked(receiver.getPicked());
		try {
			win.doModal();
		} catch (Exception e) {
		}
	}
	
	public void setFilter(String cardSerial) {
		Card card = Services.getCardService().getCardBySerialNumber(cardSerial);
		if (card == null || !card.isEnable()) {
			UIUtils.alert("无效卡");
			return;
		}
		cardTextbox.setValue(card.getCardNumber());
//		if (card.getType() != Card.TYPE_MEMBER) {
//			UIUtils.alert("不是会员卡");
//			return;
//		}
//		Member m = Services.getMemberService().getMemberByCardNumber(card.getCardNumber());
//		if (m == null) {
//			UIUtils.alert("无效会员卡");
//			return;
//		}
	}

}
